//
//  SearchBlueViewController.m
//  LazyCat
//
//  Created by jike on 15/12/17.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//

#import "SearchBlueViewController.h"
#import <CoreBluetooth/CoreBluetooth.h>
#import "MSBluetoothManager.h"
#import "FeWifiManHub.h"

#import "FeWifiManHub.h"

@interface SearchBlueViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    FeWifiManHub *_loading;
}
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *countArr;

@end

@implementation SearchBlueViewController

-(void)viewWillAppear:(BOOL)animated {
    NSString *swit = [ NSUserDefualManager getValueByKey:@"switch"];
    if ([swit isEqualToString:@"on"]) {
        [[MSBluetoothManager shareInstance] startScan];
        [_loading removeFromSuperview];
        _loading = [[FeWifiManHub alloc] initWithView:self.view withMode:FeWifiManHubModeOnlyLoader];
        [self.view addSubview:_loading];
        [_loading show];
        [self  performSelector:@selector(getVersionMessg) withObject:nil afterDelay:10];
    }
}
-(void)notice {
    self.countArr = [MSBluetoothManager shareInstance].nDevices;
    if (self.countArr.count > 0) {
        [_loading dismiss];
    }
    [self.tableView reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNav:@"搜索打印机" doBack:YES];
    UIView *lineV = [[UIView alloc]initWithFrame:CGRectMake(0, 63, ScreenWidth, 1)];
    lineV.backgroundColor = [UIColor lightGrayColor];
    lineV.alpha = 0.2;
    [self.view addSubview:lineV];
    self.countArr = [NSMutableArray array];
    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    //添加当前类对象为一个观察者，name和object设置为nil，表示接收一切通知
    [center addObserver:self selector:@selector(notice) name:@"123" object:nil];
    [self createTable];
}
- (void)scan:(UISwitch *)sender {// 搜索周边蓝牙
    if (sender.on) {
        [[MSBluetoothManager shareInstance] startScan];
        self.countArr = [MSBluetoothManager shareInstance].nDevices;
        NSUserDefaults *userDefaul = [NSUserDefaults standardUserDefaults];
        [userDefaul setObject:@"on" forKey:@"switch"];
        [_loading removeFromSuperview];
        _loading = [[FeWifiManHub alloc] initWithView:self.view withMode:FeWifiManHubModeOnlyLoader];
        [self.view addSubview:_loading];
        [_loading show];
        [self  performSelector:@selector(getVersionMessg) withObject:nil afterDelay:10];
    }else{
        [_loading dismiss];
        NSUserDefaults *userDefaul = [NSUserDefaults standardUserDefaults];
        [userDefaul setObject:@"off" forKey:@"switch"];
        self.countArr = [NSMutableArray array];
        [self.tableView reloadData];
    }
}
-(void)getVersionMessg {
    [_loading dismiss];
}
-(void)createTable {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 104, ScreenWidth, ScreenHeight - 100) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 74, ScreenWidth, 50)];
    topView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:topView];
    UISwitch *swit = [[UISwitch alloc] initWithFrame:CGRectMake(ScreenWidth - 60, 10, 60, 20)];
    swit.onTintColor=  MainColor;
    NSString *cid = [ NSUserDefualManager getValueByKey:@"switch"];
    if ([cid isEqualToString:@"on"]) {
        swit.on = YES;
    }else{
        swit.on = NO;
    }
    [swit addTarget:self action:@selector(scan:) forControlEvents:UIControlEventValueChanged];
    [topView addSubview:swit];
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(15, 15, 150, 20)];
    lable.font = [UIFont fontWithName:nil size:15];
    lable.text = @"蓝牙打印";
    [topView addSubview:lable];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.countArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    NSArray *temArr = [MSBluetoothManager shareInstance].BlueArr;
    if (temArr.count > 0) {
        NSString *blueName =temArr[indexPath.row];
        if (blueName.length < 1) {
            blueName = @"iphone";
        }
        cell.textLabel.text = blueName;
        cell.textLabel.font = [UIFont systemFontOfSize:15];
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {// 点击链接
    NSLog(@"%@",[MSBluetoothManager shareInstance].BlueArr);
    NSUserDefaults *userDefaul = [NSUserDefaults standardUserDefaults];
    NSArray *temArr = [MSBluetoothManager shareInstance].BlueArr;
    if (temArr.count > 0) {
        NSString *str = [NSString stringWithFormat:@"%@",temArr[indexPath.row]];
        [userDefaul setObject:str  forKey:@"bluename"];
    }
    NSMutableArray *sUUIDArr = [MSBluetoothManager shareInstance].sUUIDArr;
    NSMutableArray *cUUIDArr = [MSBluetoothManager shareInstance].cUUIDArr;
//    NSLog(@"=========%@",sUUIDArr);  //18F0
//    NSLog(@"=========%@",cUUIDArr);
//    "49535343-1E4D-4BD9-BA61-23C647249616",
//    "49535343-8841-43F4-A8D4-ECBE34729BB3",
//    2AF0,
//    2AF1,
//    "BEF8D6C9-9C21-4C9E-B632-BD58C1009F9F",
//    "Serial Number String",
//    "Software Revision String",
//    "Hardware Revision String",
//    "Manufacturer Name String",
//    "Model Number Stri
    if (sUUIDArr.count > 0 && cUUIDArr.count > 3) {
        NSString *suuid = [NSString stringWithFormat:@"%@",sUUIDArr[indexPath.row]];
        NSString *cuuid = [NSString stringWithFormat:@"%@",cUUIDArr[indexPath.row * 10 + 3]];
        NSLog(@"=========%@",suuid);
        NSLog(@"=========%@",cuuid);
        [userDefaul setObject:suuid forKey:@"suuid"];
        [userDefaul setObject:cuuid forKey:@"cuuid"];
        [[MSBluetoothManager shareInstance] startconnect];
    }
}
@end
